import pygame
import time
import hpbars
#import SpriteLoader

battleScreenWidth = 256
battleScreenHeight = 144
POKESWITCHTICKS = 20
HEALTHBARTICKS = 60
BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
PAUSETICKS = 15

class BattleScreen:
    isAttacking = False
    
    def __init__(self, play0, play1, tbox):
       self.battleScreen = pygame.Surface([battleScreenWidth, battleScreenHeight])
       
       self.backgroundImage = pygame.image.load("../images/BattleBackgrounds/background.png")
       
       self.secondBackgroundImage = pygame.image.load("../images/BattleBackgrounds/sprite5.png")
       self.secondBackgroundImage.set_colorkey([255, 255, 255])
       
       self.play = [play0, play1]
       self.hpBars = [hpbars.HPBar(0), hpbars.HPBar(1)]
       self.textbox = tbox
       
       self.hpBars[0].setNew(self.play[0].pokemon[self.play[0].activePoke].name, self.play[0].pokemon[self.play[0].activePoke].HP_stat[0], self.play[0].pokemon[self.play[0].activePoke].HP_stat[1], None)
       self.hpBars[1].setNew(self.play[1].pokemon[self.play[1].activePoke].name, self.play[1].pokemon[self.play[1].activePoke].HP_stat[0], self.play[1].pokemon[self.play[1].activePoke].HP_stat[1], None)
       
       self.pokeFrontImage = pygame.image.load('../images/fronts/' + str(self.play[1].pokemon[self.play[1].activePoke].ID) + '.png')#
       self.topPokePos = [192 - (self.pokeFrontImage.get_size()[0] / 2 ), 95 - (self.pokeFrontImage.get_size()[1])]
       
       self.pokeBackImage = pygame.image.load('../images/back/' + str(self.play[0].pokemon[self.play[0].activePoke].ID) + '.png')
       self.bottomPokePos = [64 - (self.pokeBackImage.get_size()[0] / 2), battleScreenHeight - (self.pokeBackImage.get_size()[1] / 1.5)]
       
       self.animationFrames = [10, 17, 23, 10, 30, 17, 17, 11, 14, 11, 11, 4, 13, 4]
       self.animationHeight = [144, 191, 191, 144, 144, 191, 144, 191, 191, 144, 191, 191, 191, 32]
       self.animationHeightv2 = [144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 32]
       self.animationWidth = [256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 32]
       self.animationRow = [10, 17, 23, 4, 30, 17, 17, 11, 14, 11, 11, 4, 13, 4]
       self.animationColumn = [1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
       ###############       0                 1                 2               3          4            5            6               7          8             9                      10           11                12         13
       self.animation = ['FIRE-BLAST-1', 'HYDRO-CANNON-2', 'SHADOW-BALL-1', 'Seed-Flare', 'SURFV2', 'MUD-SHOT-1', 'AEROBLAST-1', 'SWIFT-1', 'HYDRO-PUMP-1', 'FRENZY-PLANT-1', 'BLAST-BURN-1', 'WOOD-HAMMER-1', 'MUDSPORT', 'THUNDAR']
       self.animationBackground = ['fireblastybackground', 'Hydro-cannon-back', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'AeroBlastBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground', 'whiteBackground']
       self.animationColor = [BLACK, BLACK, BLACK, WHITE, WHITE, WHITE, BLACK, BLACK, BLACK, WHITE, BLACK, BLACK, WHITE, BLACK]

       self.currentAnim = "None"
       self.attackBackgroundImage = None
       self.attackForegroundImage = None
       
    def display(self):
        self.battleScreen = pygame.Surface([battleScreenWidth, battleScreenHeight])
        self.battleScreen.blit(self.backgroundImage, [0, 0])
        self.battleScreen.blit(self.secondBackgroundImage, [0, 0])
        
        if(self.attackBackgroundImage != None):
            self.battleScreen.blit(self.attackBackgroundImage, [0, 0])
        
        self.battleScreen.blit(self.pokeFrontImage, self.topPokePos)
        self.battleScreen.blit(self.pokeBackImage, self.bottomPokePos)
        
        if(self.attackForegroundImage != None):
            temp = self.attackForegroundImage
            temp.set_colorkey(self.animationColor[self.animNum])
            self.battleScreen.blit(temp, [0, 0])
        
        if(self.currentAnim != "Attack"):
            topHealthBar = self.hpBars[0].getDisp()
            topHealthBar.set_colorkey((189, 189, 231))
            self.battleScreen.blit(topHealthBar, [battleScreenWidth - self.hpBars[0].mainGraphic.get_size()[0] + 1, battleScreenHeight - 5 - self.hpBars[0].mainGraphic.get_size()[1]])
            topHealthBar = self.hpBars[1].getDisp()
            topHealthBar.set_colorkey((189, 189, 231))
            self.battleScreen.blit(topHealthBar, [-3, 30])
        
        self.battleScreen = pygame.transform.scale2x(self.battleScreen)
        
        return self.battleScreen
    
    def newPokes(self, poke1, poke2):
        self.pokeBackImage = pygame.image.load("../images/back/" + str(poke1.ID) + ".png")
        self.hpBars[0].setNew(poke1.name, poke1.HP_stat[0], poke1.HP_stat[1], None)
        self.bottomPokePos = [64 - (self.pokeBackImage.get_size()[0] / 2), battleScreenHeight - (self.pokeBackImage.get_size()[1] / 1.5)]
        self.pokeFrontImage = pygame.image.load("../images/fronts/" + str(poke2.ID) + ".png")
        self.hpBars[0].setNew(poke2.name, poke2.HP_stat[0], poke2.HP_stat[1], None)
        self.topPokePos = [192 - (self.pokeFrontImage.get_size()[0] / 2 ), 95 - (self.pokeFrontImage.get_size()[1])]
    
    def newPoke(self, playNum, newPokeNum):
        if(playNum == 0):
            self.newPokeInfo = newPokeNum
            self.ticksLeft = POKESWITCHTICKS
            self.currentAnim = "BottomPokeOut"
            self.newPokeBackImage = pygame.image.load('../images/back/' + str(newPokeNum.ID) + '.png')
            self.exitRate = ((self.bottomPokePos[0] + self.pokeBackImage.get_size()[0]) // self.ticksLeft) + 1
        if(playNum == 1):
            self.newPokeInfo = newPokeNum
            self.ticksLeft = POKESWITCHTICKS
            self.currentAnim = "TopPokeOut"
            self.newPokeFrontImage = pygame.image.load('../images/fronts/' + str(newPokeNum.ID) + '.png')
            self.exitRate = (battleScreenWidth - self.topPokePos[0]) // self.ticksLeft + 1
            
    def newAttack(self, playNum, newHealth1, newHealth2, moveID, animRun = True):
        if(animRun == True):
            self.currentAnim = "Attack"
            self.animNum = 6
            self.attackForegroundImage = pygame.image.load('../images/attackAnimations/' + self.animation[self.animNum] + '.bmp')
            if(self.animationBackground[self.animNum] == "whiteBackground"):
                self.attackBackgroundImage = None
            else:
                self.attackBackgroundImage = pygame.image.load('../images/attackAnimations/' + self.animationBackground[self.animNum] + '.png')
            
            self.attackSprite = []
            
            for ROW in range(self.animationRow[self.animNum]):
                for COLUMN in range(self.animationColumn[self.animNum]):
                    nextFrame = pygame.Surface((self.animationWidth[self.animNum], self.animationHeightv2[self.animNum]))
                    nextFrame.blit(self.attackForegroundImage, (0, 0), (COLUMN * self.animationWidth[self.animNum], ROW * self.animationHeight[self.animNum], COLUMN * self.animationWidth[self.animNum] + (self.animationWidth[self.animNum] - 1), ROW * self.animationHeight[self.animNum] + (self.animationHeight[self.animNum] - 1)))
                    nextFrame.set_colorkey(self.animationColor[self.animNum])
                    self.attackSprite.append(nextFrame)
                    if(len(self.attackSprite) == self.animationFrames[self.animNum]):
                        break
                if(len(self.attackSprite) == self.animationFrames[self.animNum]):
                    break
            
            self.frame = 0
            
            self.playNum = playNum
            self.nHealth1 = newHealth1
            self.nHealth2 = newHealth2
            self.moveID = moveID
            self.ticksLeft = len(self.attackSprite) * 4
        else:
            self.nHealth1 = newHealth1
            self.nHealth2 = newHealth2
            if(self.hpBars[0].cHealth != self.nHealth1):
                self.ticksLeft = HEALTHBARTICKS
                self.currentAnim = "PlayerHealthBar"
                self.attackBackgroundImage = None
                self.attackForegroundImage = None
            elif(self.hpBars[1].cHealth != self.nHealth2):
                self.ticksLeft = HEALTHBARTICKS
                self.currentAnim = "OppHealthBar"
                self.attackBackgroundImage = None
                self.attackForegroundImage = None
            

    def update(self):
        if(self.currentAnim == "Attack"):
            if(self.ticksLeft == 0):
                if(self.hpBars[0].cHealth != self.nHealth1):
                    self.ticksLeft = HEALTHBARTICKS
                    self.currentAnim = "PlayerHealthBar"
                    self.attackBackgroundImage = None
                    self.attackForegroundImage = None
                elif(self.hpBars[1].cHealth != self.nHealth2):
                    self.ticksLeft = HEALTHBARTICKS
                    self.currentAnim = "OppHealthBar"
                    self.attackBackgroundImage = None
                    self.attackForegroundImage = None
                else:
                    self.currentAnim = "Wait"
                    self.ticksLeft = PAUSETICKS
                    self.attackBackgroundImage = None
                    self.attackForegroundImage = None
            elif(self.ticksLeft % 4 == 0):
                if(self.frame > len(self.attackSprite) - 1):
                    self.ticksLeft = 0
                else:
                    self.attackForegroundImage = self.attackSprite[self.frame]
                    self.frame += 1
            self.ticksLeft -= 1
        elif(self.currentAnim == "Wait"):
            if(self.ticksLeft == 0):
                self.currentAnim = "None"
                return True
            self.ticksLeft -= 1
        elif(self.currentAnim == "PlayerHealthBar"):
            if(self.ticksLeft == 0):
                if(self.hpBars[1].cHealth != self.nHealth2):
                    self.ticksLeft = HEALTHBARTICKS
                    self.currentAnim = "OppHealthBar"
                else:
                    self.currentAnim = "Wait"
                    self.ticksLeft = PAUSETICKS
                    return
            rate = (self.hpBars[0].cHealth - self.nHealth1) // self.ticksLeft
            self.hpBars[0].updateHealth(self.hpBars[0].cHealth - rate)
            self.ticksLeft -= 1
        elif(self.currentAnim == "OppHealthBar"):
            if(self.ticksLeft == 0):
                self.currentAnim = "Wait"
                self.ticksLeft = PAUSETICKS
                return
            rate = (self.hpBars[1].cHealth - self.nHealth2) // self.ticksLeft
            self.hpBars[1].updateHealth(self.hpBars[1].cHealth - rate)
            self.ticksLeft -= 1
        elif(self.currentAnim == "BottomPokeOut"):
            if(self.ticksLeft == 0):
                self.ticksLeft = POKESWITCHTICKS
                self.pokeBackImage = self.newPokeBackImage
                self.bottomPokePos = [-self.pokeBackImage.get_size()[0], battleScreenHeight - (self.pokeBackImage.get_size()[1] / 1.5)]
                self.bottomPokePosF = [64 - (self.pokeBackImage.get_size()[0] / 2), battleScreenHeight - (self.pokeBackImage.get_size()[1] / 1.5)]
                self.enterRate = (self.bottomPokePosF[0] - self.bottomPokePos[0]) // 30
                self.currentAnim = "BottomPokeIn"
                self.hpBars[0].setNew(self.newPokeInfo.name, self.newPokeInfo.HP_stat[0], self.newPokeInfo.HP_stat[1], None)
            self.bottomPokePos = [self.bottomPokePos[0] - self.exitRate, self.bottomPokePos[1]]
            self.ticksLeft -= 1
        elif(self.currentAnim == "BottomPokeIn"):
            self.bottomPokePos = [self.bottomPokePos[0] + self.enterRate, self.bottomPokePos[1]]
            self.ticksLeft -= 1
            if(self.ticksLeft == 0):
                self.bottomPokePos = self.bottomPokePosF
                self.currentAnim = "None"
                return True
            self.enterRate = (self.bottomPokePosF[0] - self.bottomPokePos[0]) // self.ticksLeft
        elif(self.currentAnim == "TopPokeOut"):
            if(self.ticksLeft == 0):
                self.ticksLeft = POKESWITCHTICKS
                self.pokeFrontImage = self.newPokeFrontImage
                self.topPokePos = [battleScreenWidth, 95 - (self.pokeFrontImage.get_size()[1])]
                self.topPokePosF = [192 - (self.pokeFrontImage.get_size()[0] / 2 ), 95 - (self.pokeFrontImage.get_size()[1])]
                self.enterRate = (self.topPokePosF[0] - self.topPokePos[0]) // self.ticksLeft
                self.hpBars[1].setNew(self.newPokeInfo.name, self.newPokeInfo.HP_stat[0], self.newPokeInfo.HP_stat[1], None)
                self.currentAnim = "TopPokeIn"
            self.topPokePos = [self.topPokePos[0] + self.exitRate, self.topPokePos[1]]
            self.ticksLeft -= 1
            return False
        elif(self.currentAnim == "TopPokeIn"):
            self.topPokePos = [self.topPokePos[0] + self.enterRate, self.topPokePos[1]]
            self.ticksLeft -= 1
            if(self.ticksLeft == 0):
                self.topPokePos = self.topPokePosF
                self.currentAnim = "None"
                return True
            self.enterRate = (self.topPokePosF[0] - self.topPokePos[0]) // self.ticksLeft
        elif(self.currentAnim == "None"):
            return True
        return False
                
        